package com.yangfan.juc.atomic;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description:
 * @author: yangfan
 * @date: Created in 2020/3/19 0019 14:51
 * @modified By:
 */
public class AtomicIntegerTest {

    static AtomicInteger value = new AtomicInteger();

    static Set<Integer> set = Collections.synchronizedSet(new HashSet<Integer>());


    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(){
            @Override
            public void run() {
                int x = 0;
                while (x < 500){
                    int v = value.getAndIncrement();
                    set.add(v);
                    System.out.println(Thread.currentThread().getName()+":"+ v);
                    x++;
                }
            }
        };

        Thread t2 = new Thread(){
            @Override
            public void run() {
                int x = 0;
                while (x < 500){
                    int v = value.getAndIncrement();
                    set.add(v);
                    System.out.println(Thread.currentThread().getName()+":"+ v);
                    x++;
                }
            }
        };

        Thread t3 = new Thread(){
            @Override
            public void run() {
                int x = 0;
                while (x < 500){
                    int v = value.getAndIncrement();
                    set.add(v);
                    System.out.println(Thread.currentThread().getName()+":"+ v);
                    x++;
                }
            }
        };






        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();

        System.out.println(set.size());

    }





}
